package com.taptech.doufu.weex;

/**
 * Created by qlx on 2016/12/12.
 */

import android.animation.Animator;
import android.animation.AnimatorInflater;
import android.animation.AnimatorSet;
import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewStub;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.squareup.picasso.Picasso;
import com.taptech.doufu.ui.activity.weex.TFWXActivity;
import com.taptech.doufu.R;


import java.lang.ref.WeakReference;

/**
 * Created by longsiyang on 16/2/23.
 */
public class TFGiftAnimationUtil {

    private static TFGiftAnimationUtil instance = null;

    Context mContext;
    RelativeLayout giftLayout;
    ImageView giftBackView , giftTopView;
    SimpleDraweeView giftView;
    TextView topTextView;

    AnimatorSet animatorSet;
    AnimatorSet animatorSet2;
    Animator giftViewAnim , giftBackAnim , giftTopAnim , giftTopTextAnim;

    View rootView = null;
    private ViewStub mGiftGuideStub;
    private String giftDesc = "";
    private String giftId;

    PopupWindow giftPopupWindow = null;

    /**
     * 加载中的动画
     * @param url
     */
    public static void loading(Context context, String url){
        instance = new TFGiftAnimationUtil(context);
        instance.animStart(url);
    }

    /**
     * 结束动画
     * @param msg  结束显示文本
     * @param animated （是否直接关闭） 如果为false 就直接关闭，不会显示文本
     */
    public static void  close(final String msg , final Boolean animated){
        if (animated == true){
            new Handler().postDelayed(new Runnable(){
                public void run() {
                    TFGiftAnimationUtil.excuteClose(msg,animated);
                }
            }, 1500);
        }else {
            TFGiftAnimationUtil.excuteClose(msg,animated);
        }

    }

    public static void  excuteClose(String msg ,Boolean animated) {
        if (instance != null){

            if (animated == false){
                instance.giftPopupWindow.dismiss();
            }else {
                if (msg instanceof String ){
                    if (msg.length() > 0){
                        instance.topTextView.setText(msg);
                    }
                }

                instance.animatorSet.cancel();
                instance.initAnim2();
                instance.animatorSet2.start();
            }


        }
    }


    public TFGiftAnimationUtil(Context context){
        mContext = context;
        this.rootView = LayoutInflater.from(context).inflate(R.layout.gift_animation_view, null);
        giftPopupWindow = new PopupWindow(rootView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        giftPopupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        View deV =((Activity)mContext).getWindow().getDecorView();
        giftPopupWindow.showAtLocation(deV, Gravity.BOTTOM | Gravity.CENTER, 0, 0);
        giftPopupWindow.setFocusable(true);
        giftPopupWindow.setOutsideTouchable(true);

        giftLayout = (RelativeLayout) rootView.findViewById(R.id.gift_layout_id);
        giftView = (SimpleDraweeView) rootView.findViewById(R.id.gift_view);
        giftBackView = (ImageView) rootView.findViewById(R.id.gift_back_view);
        giftTopView = (ImageView) rootView.findViewById(R.id.gift_top_view);
        topTextView = (TextView) rootView.findViewById(R.id.gift_top_text);
        mGiftGuideStub = (ViewStub) rootView.findViewById(R.id.gift_guide_stub);
        //initAnim();
    }


    public void initAnim2(){
        giftViewAnim = AnimatorInflater.loadAnimator(mContext, R.animator.gift_anim_gift_img3);
        giftViewAnim.setTarget(giftView);
        giftBackAnim = AnimatorInflater.loadAnimator(mContext, R.animator.gift_anim_gift_back3);
        giftBackAnim.setTarget(giftBackView);
        giftTopAnim = AnimatorInflater.loadAnimator(mContext, R.animator.gift_anim_gift_top2);
        giftTopAnim.setTarget(giftTopView);

        giftTopTextAnim = AnimatorInflater.loadAnimator(mContext, R.animator.gift_anim_top_text2);
        giftTopTextAnim.setTarget(topTextView);
        animatorSet2 = new AnimatorSet();
        giftTopTextAnim.setDuration(1000);
        animatorSet2.playTogether(giftViewAnim , giftBackAnim,giftTopAnim,giftTopTextAnim);
        final WeakReference weakSelf = new WeakReference(this);
        animatorSet2.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                try {
                    TFGiftAnimationUtil ga = (TFGiftAnimationUtil) weakSelf.get();
                    ga.giftPopupWindow.dismiss();
                    instance = null;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
    }


    public void initAnim(){
        giftViewAnim = AnimatorInflater.loadAnimator(mContext, R.animator.gift_anim_gift_img2);
        giftViewAnim.setTarget(giftView);
        giftBackAnim = AnimatorInflater.loadAnimator(mContext, R.animator.gift_anim_gift_back2);
        giftBackAnim.setTarget(giftBackView);

        giftTopView.setAlpha(0.0f);
        topTextView.setAlpha(0);
        animatorSet = new AnimatorSet();

        animatorSet.playTogether(giftViewAnim , giftBackAnim);
        final WeakReference weakSelf = new WeakReference(this);
        animatorSet.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {

            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
    }

    public void  animStart(String url){
        initAnim();
        Picasso.with(mContext).load(url).into(giftView);
        giftLayout.setVisibility(View.VISIBLE);
        animatorSet.start();

    }
}
